package com.ktgj.common.core.utils.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;

public class SimpleConverter
{
	private final static Logger LOGGER = LoggerFactory.getLogger(SimpleConverter.class);

	@SuppressWarnings("deprecation")
	public static Object convert(Object value, Class<?> targetType)
	{
		if (value == null)
		{
			return null;
		}

		if (targetType.equals(value.getClass()))
		{
			return value;
		}

		if (targetType.isAssignableFrom(value.getClass()))
		{
			return value;
		}

		if (targetType.equals(Boolean.TYPE))
		{
			return Boolean.parseBoolean(value.toString());
		}

		if (targetType.equals(String.class))
		{
			return value.toString();
		}

		if (targetType.equals(Integer.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			try
			{
				return Integer.parseInt(value.toString());
			}
			catch (Exception e)
			{
				return (int) (double) (Double.parseDouble(value.toString()));
			}
		}

		if (targetType.equals(Double.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return Double.parseDouble(value.toString());
		}

		if (targetType.equals(Float.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return Float.parseFloat(value.toString());
		}

		if (targetType.equals(Byte.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return Byte.parseByte(value.toString());
		}

		if (targetType.equals(Short.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return Short.parseShort(value.toString());
		}

		if (targetType.equals(java.util.Date.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return new java.util.Date(value.toString());
		}

		if (targetType.equals(java.sql.Date.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return java.sql.Date.parse(value.toString());
		}

		if (targetType.equals(java.sql.Time.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			return java.sql.Time.parse(value.toString());
		}

		if (targetType.equals(BigInteger.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			if (value.getClass().equals(long.class)) return BigInteger.valueOf((long) value);

			return BigInteger.valueOf(Long.parseLong(value.toString()));
		}

		if (targetType.equals(BigDecimal.class))
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			if (value.getClass().equals(long.class)) return BigDecimal.valueOf((long) value);
			if (value.getClass().equals(double.class)) return BigDecimal.valueOf((double) value);
			return BigDecimal.valueOf(Double.parseDouble(value.toString()));
		}

		if (targetType.isEnum())
		{
			if (StringHelper.isNullOrEmpty(value.toString())) return null;
			try
			{
				Field field = targetType.getField(value.toString());
				return field.get(null);
			}
			catch (Exception e)
			{
				Field[] fields = targetType.getFields();
				if (fields.length > 0)
				{
					try
					{
						return fields[0].get(null);
					}
					catch (Exception ex)
					{
						LOGGER.error(ex.getMessage());
					}
				}
				else
				{
					LOGGER.error(e.getMessage());
				}
			}
		}

		return value;
	}
}
